<div style="background-color:#EEE"><a href="./index.php?act=admin">Admin</a> > <a href="/index.php?act=admin&do=places">Places</a></div>
<?php
if(isset($_REQUEST['edit'])){
	if(!isset($_REQUEST['year'])) $year = date('Y');
	else $year = $_REQUEST['year'];
	
	if(isset($_POST['image_upload'])){
		if(!empty($_FILES['file'])){
			$tmp_name = $_FILES['file']['tmp_name'];
			$i = pathinfo($_FILES['file']['name']);
			$name = strtolower(generateCode(20));
			$result = 0;
			if(move_uploaded_file($tmp_name, "uploads/$name.".$i['extension'])){
				mysql_query("INSERT INTO `uploads` (`placeid`, `filename`) VALUES (".$_REQUEST['edit'].",'".$name.".".$i['extension']."' )");
			}
		}
	}
	
	if(isset($_POST['save_place'])){
		if(mysql_query("UPDATE `places` SET `name` = '".$_REQUEST['name']."',`coordinates` = '".$_REQUEST['coordinates']."' WHERE `id` = ".$_REQUEST['edit'])){
			$tmp = mysql_query("SELECT `id` FROM `descriptions` WHERE `year` = '".$year."' AND `placeid` = ".$_REQUEST['edit']);
			if(mysql_num_rows($tmp) == 1){
				mysql_query("UPDATE `descriptions` SET `description` = '".$_REQUEST['description']."',`long_description` = '".$_REQUEST['long_description']."' WHERE `year` = '".$year."' AND `placeid` = ".$_REQUEST['edit']);
			}
			else mysql_query("INSERT INTO `descriptions` (`placeid`,`description`,`long_description`,`year`) VALUES ('".$_REQUEST['edit']."','".$_REQUEST['description']."','".$_REQUEST['long_description']."','".$_REQUEST['year']."')");
			
			echo '<div class="success">Save Success</div>';
		}
		else echo '<div class="failed">Save Failed</div>';
	}
	
	$tmp = mysql_query("SELECT * FROM `places` WHERE `id` = ".$_REQUEST['edit']);
	$getPlaces = mysql_fetch_assoc($tmp);
	
	$tmpUploadResult = mysql_query("SELECT * FROM `uploads` WHERE `placeid` = ".$_REQUEST['edit']);
	
	/* For Descriptions */
	$tmp = mysql_query("SELECT * FROM `descriptions` WHERE `year` = '".$year."' AND `placeid` = ".$_REQUEST['edit']);
	$getDescription = mysql_fetch_assoc($tmp);
	
	$i = 0;
	$tmpUploadResult = mysql_query("SELECT * FROM `uploads` WHERE `placeid` = ".$_REQUEST['edit']);
	while($getUploads = mysql_fetch_assoc($tmpUploadResult)){
		if($i!=0) $attachments .= ',';
		$attachments .= '"'.$getUploads['filename'].'"'; $i++;
	}
?>

<script type="text/javascript">var attachments = [<?=$attachments?>];</script>
<form action="" method="post" enctype="multipart/form-data">
<fieldset style="padding:5px; border:1px solid #ccc;">
<legend align="right"><button type="submit" name="save_place">Save</button></legend>
<div style="margin-top:15px;">
<label for="name">Place Name</label> <input name="name" id="name" type="text" value="<?=$getPlaces['name']?>" style="padding:5px;">
</div>
<div style="margin-top:15px;">
<label for="name">Coordinates</label> <input name="coordinates" id="coordinates" type="text" value="<?=$getPlaces['coordinates']?>" style="padding:5px;">
</div>
<fieldset style="border:1px dashed #AAA; padding:20px;">
<legend>
<select name="year" onchange="submit()">
<?php
for($i=2020; $i>=2000; $i--){
	echo '<option value="'.$i.'" '.($i==$year?'selected="selected"':'').'>'.$i.'</option>';
}?>
</select>
</legend>
<div style="margin-top:15px;">
<label for="description">Description</label>
<textarea name="description" id="description" class="rte-zone"><?=$getDescription['description']?></textarea>
</div>
<div style="margin-top:15px;">
<label for="description">Long Description</label>
<textarea name="long_description" id="long_description" class="rte-zone"><?=$getDescription['long_description']?></textarea>
</div>
</fieldset>
<div style="margin-top:15px;">
</div>

<div style="margin-top:15px;">
Image Upload<br />
<input style="padding:5px; border:1px solid #ccc;" name="file" id="file" type="file" /> <button type="submit" name="image_upload">Upload</button>
</div>
</fieldset>
</form>

<?php
} else {
?>
<table id="listofplaces" width="100%" border="0">
  <tr>
    <th colspan="2" align="left">List of Places</th>
  </tr>  
  <tr>
    <td width="7%">&nbsp;</td>
    <th width="93%" align="left">Name</th>
  </tr>
  <tbody>
  <?php
  $result = mysql_query("SELECT * FROM `places`");
  while($row = mysql_fetch_assoc($result)){
	  echo '<tr>
		<td width="7%">'.$row['id'].'</td>
		<td width="93%"><a href="./index.php?act=admin&do=places&edit='.$row['id'].'">'.$row['name'].'</a></td>
	  </tr>';
  }  
  ?>
  </tbody>
</table>

<?php } ?>

<script type="text/javascript">
$(".rte-zone").rte({
    content_css_url: "includes/rte-light/rte.css",
    media_url: "includes/rte-light/",
});
</script>